package org.jeecg.modules.exp.controller;

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.exp.entity.ExpAccount;
import org.jeecg.modules.exp.entity.ExpUsageApplying;
import org.jeecg.modules.exp.entity.ExpUsageDetail;
import org.jeecg.modules.exp.entity.ExpUsageMiddle;
import org.jeecg.modules.exp.service.IExpUsageApplyingService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.exp.service.IExpUsageMiddleService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 专家管理（专家使用申请表）
 * @Author: jeecg-boot
 * @Date:   2022-11-28
 * @Version: V1.0
 */
@Api(tags="专家管理（专家使用申请表）")
@RestController
@RequestMapping("/exp/expUsageApplying")
@Slf4j
public class ExpUsageApplyingController extends JeecgController<ExpUsageApplying, IExpUsageApplyingService> {
	@Autowired
	private IExpUsageApplyingService expUsageApplyingService;

	 @Autowired
	 private IExpUsageMiddleService expUsageMiddleService;


	 /**
	 * 分页列表查询
	 *
	 * @param expUsageApplying
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "专家管理（专家使用申请表）-分页列表查询")
	@ApiOperation(value="专家管理（专家使用申请表）-分页列表查询", notes="专家管理（专家使用申请表）-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ExpUsageApplying expUsageApplying,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ExpUsageApplying> queryWrapper = QueryGenerator.initQueryWrapper(expUsageApplying, req.getParameterMap());
		Page<ExpUsageApplying> page = new Page<>(pageNo, pageSize);
		IPage<ExpUsageApplying> pageList = expUsageApplyingService.page(page, queryWrapper);

		Page<ExpUsageDetail> detailPage = new Page<>(pageNo, pageSize);
		List<ExpUsageDetail> details = new ArrayList<>();
		for(ExpUsageApplying usage: pageList.getRecords()) {
			List<ExpUsageMiddle> experts = expUsageApplyingService.queryExpertByUsageID(usage.getId());
			ExpUsageDetail detail = new ExpUsageDetail();
			detail.setExpUsageApplying(usage);
			detail.setExpUsageMiddleList(experts);

			details.add(detail);
		}
		detailPage.setRecords(details);
		return Result.OK(detailPage);
	}

	/**
	 *   添加
	 *
	 * @param expUsageDetail
	 * @return
	 */
	@AutoLog(value = "专家管理（专家使用申请表）-添加")
	@ApiOperation(value="专家管理（专家使用申请表）-添加", notes="专家管理（专家使用申请表）-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody ExpUsageDetail expUsageDetail) {
		//先添加专家使用信息，然后将这条专家使用信息的ID设为中间记录的外ID
		expUsageApplyingService.save(expUsageDetail.getExpUsageApplying());
		String id = expUsageDetail.getExpUsageApplying().getId();
		List<ExpUsageMiddle> expUsageMiddleList = expUsageDetail.getExpUsageMiddleList();
		for(ExpUsageMiddle middle: expUsageMiddleList) {
			middle.setUsageId(id);
			expUsageMiddleService.save(middle);
		}
		return Result.OK("添加成功！");
	}

	 @AutoLog(value = "专家管理（专家使用申请表）-获取所有专家信息")
	 @ApiOperation(value="专家管理（专家使用申请表）-获取所有专家信息", notes="专家管理（专家使用申请表）-获取所有专家信息")
	 @GetMapping(value = "/queryAllExpert")
	 public Result<?> queryAllExpert() {
		List<ExpAccount> exps = expUsageApplyingService.queryAllExpert();
		return Result.OK(exps);
	 }

	 @AutoLog(value = "专家管理（专家使用申请表）-通过ID获取专家信息")
	 @ApiOperation(value="专家管理（专家使用申请表）-通过ID获取专家信息", notes="专家管理（专家使用申请表）-通过ID获取专家信息")
	 @PostMapping(value = "/queryExpertByID")
	 public Result<?> queryExpertByID(@RequestParam(name="id",required=true) String id) {
		 List<ExpUsageMiddle> exps = expUsageApplyingService.queryExpertByExpertID(id);
		 Map<String, List<Date>> expUsageDuration = new HashMap<>();
		 for (ExpUsageMiddle middle: exps) {
			 String usageID = middle.getUsageId();
			 if (usageID == null)
				 continue;
			 ExpUsageApplying expUsageApplying = expUsageApplyingService.getById(usageID);
			 if(expUsageApplying == null)
				 continue;
			 expUsageDuration.put(expUsageApplying.getProjectNameNumber(),
					 new ArrayList<>(Arrays.asList(middle.getStartTime(), middle.getEndTime())));
		 }
		 return Result.OK(expUsageDuration);
	 }

	/**
	 *  编辑
	 *
	 * @param expUsageDetail
	 * @return
	 */
	@AutoLog(value = "专家管理（专家使用申请表）-编辑")
	@ApiOperation(value="专家管理（专家使用申请表）-编辑", notes="专家管理（专家使用申请表）-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody ExpUsageDetail expUsageDetail) {
		expUsageApplyingService.updateById(expUsageDetail.getExpUsageApplying());
		List<ExpUsageMiddle> middles = expUsageDetail.getExpUsageMiddleList();
		for(ExpUsageMiddle middle: middles) {
			expUsageMiddleService.updateById(middle);
		}
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "专家管理（专家使用申请表）-通过id删除")
	@ApiOperation(value="专家管理（专家使用申请表）-通过id删除", notes="专家管理（专家使用申请表）-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		expUsageApplyingService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "专家管理（专家使用申请表）-批量删除")
	@ApiOperation(value="专家管理（专家使用申请表）-批量删除", notes="专家管理（专家使用申请表）-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.expUsageApplyingService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "专家管理（专家使用申请表）-通过id查询")
	@ApiOperation(value="专家管理（专家使用申请表）-通过id查询", notes="专家管理（专家使用申请表）-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		ExpUsageApplying expUsageApplying = expUsageApplyingService.getById(id);
		if(expUsageApplying==null) {
			return Result.error("未找到对应数据");
		}
		List<ExpUsageMiddle> middles = expUsageApplyingService.queryExpertByUsageID(id);
		ExpUsageDetail detail = new ExpUsageDetail();
		detail.setExpUsageApplying(expUsageApplying);
		detail.setExpUsageMiddleList(middles);
		return Result.OK(detail);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param expUsageApplying
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ExpUsageApplying expUsageApplying) {
        return super.exportXls(request, expUsageApplying, ExpUsageApplying.class, "专家管理（专家使用申请表）");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ExpUsageApplying.class);
    }

}
